<?php

namespace App\Models\College;

use App\Models\BaseModel;
use App\Models\Common\ResourcesModel;
use App\Models\Traits\KfSoftDeletes;
use App\Service\College\CoursesService;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CollegeCourseModel extends BaseModel
{
    use KfUid;

    use KfSoftDeletes;

    protected $table = 'college_courses';

    protected $fillable = [
        'id',
        'name',
        'desc',
        'type',
        'categoryId',
        'status',
        'createdAt',
        'updatedAt',
        'createdByUid',
        'updatedByUid',
    ];

    const COURSE_STATUS_ON  = 1;
    const COURSE_STATUS_OFF = 0;


    const COURSE_STATUS_MAPPING = [
        self::COURSE_STATUS_OFF => '下架',
        self::COURSE_STATUS_ON  => '上架',
    ];

    const TYPE_IMG_TEXT = 2;
    const TYPE_VIDEO    = 1;

    const TYPE_MAPPING = [
        self::TYPE_VIDEO    => '视频',
        self::TYPE_IMG_TEXT => '图文',
    ];

    /**
     * 关联资源表
     * @return HasMany
     */
    public function resourcesRel(): HasMany
    {
        return $this->hasMany(ResourcesModel::class, 'businessId', 'id')
            ->where('module', ResourcesModel::MODULE_TYPE_COLLEGE)
            ->where('business', ResourcesModel::BUSINESS_COLLEGE_COURSES)
            ->whereIn('name', [
                CoursesService::FILE_COURSES_COVER_IMG,
                CoursesService::FILE_COURSES_WARE_VIDEO,
                CoursesService::FILE_COURSES_WARE_PDF,
                CoursesService::FILE_COURSES_WARE_IMG,
            ]);
    }

    /**
     * 关联学习进度表
     * @return HasMany
     */
    public function courseProgressRel(): HasMany
    {
        return $this->hasMany(CollegeCourseProgressModel::class, 'courseId', 'id');
    }


    /**
     * 关联分类表
     * @return HasOne
     */
    public function categoryRel(): HasOne
    {
        return $this->hasOne(CollegeCategoryModel::class, 'id', 'categoryId');
    }

    /**
     * 根据id查询课程详情
     * @param $id
     * @return Builder|Model|object|null
     */
    public static function getCoursesInfoById($id)
    {
        return self::query()
            ->leftJoin('college_category', 'college_category.id', '=', 'college_courses.categoryId')
            ->where(['college_courses.id' => $id, 'college_courses.isDeleted' => 0])
            ->select('college_courses.id', 'college_courses.name', 'college_courses.desc', 'college_courses.type', 'college_courses.categoryId', 'college_category.name as categoryName', 'college_courses.status')
            ->first();
    }

    /**
     * 新增课程
     * @param array $data
     * @return Model
     */
    public static function addCourses($data)
    {
        return self::query()->create($data);
    }

    /**
     * 修改课程
     * @param $id
     * @param $data
     * @return int
     */
    public static function setCoursesInfoById($id, $data): int
    {
        return self::query()->where(['id' => $id])->update($data);
    }
}



